﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using SIPO.Library;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.Enum;
using SIPO.Library.Interfaces;
using SIPO.Library.DomainObject.Administration.Catalog;

namespace SIPO.Library.DataAccess.Transaction
{
    public partial class recDevolutionRepo : ISearchable<recDevolution>
    {
        public object SelectScalar(SQL.Function.Aggregate function, string column, string parameters)
        {
            object _result = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    switch (function)
                    {
                        case SQL.Function.Aggregate.Max:
                            sbQuery.AppendFormat("SELECT MAX({0}) FROM transactions.recDevolution {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Min:
                            sbQuery.AppendFormat("SELECT MIN({0}) FROM transactions.recDevolution {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Distinct:
                            sbQuery.AppendFormat("SELECT COUNT({0}) FROM transactions.recDevolution {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Count:
                            sbQuery.AppendFormat("SELECT COUNT({0}) FROM transactions.recDevolution {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Sum:
                            sbQuery.AppendFormat("SELECT SUM({0}) FROM transactions.recDevolution {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Avg:
                            sbQuery.AppendFormat("SELECT AVG({0}) FROM transactions.recDevolution {1}", column, parameters);
                            break;
                        default:
                            // do nothing 
                            break;
                    }

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        _result = cmd.ExecuteScalar();
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return _result;
        }

        public object SelectScalarDevolutionProcess(SQL.Function.Aggregate function, string column, string parameters)
        {
            object _result = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    switch (function)
                    {
                        case SQL.Function.Aggregate.Max:
                            sbQuery.AppendFormat("SELECT MAX({0}) FROM transactions.recDevolutionProcess", column);
                            sbQuery.AppendFormat(@"     inner join transactions.recDevolutionProcessDetail
                                                        on recDevolutionProcessDetail.DevolutionProcessID = recDevolutionProcessDetail.DevolutionProcessID {0}", parameters);
                            break;
                        case SQL.Function.Aggregate.Min:
                            sbQuery.AppendFormat("SELECT MIN({0}) FROM transactions.recDevolutionProcess", column);
                            sbQuery.AppendFormat(@"     inner join transactions.recDevolutionProcessDetail
                                                        on recDevolutionProcessDetail.DevolutionProcessID = recDevolutionProcessDetail.DevolutionProcessID {0}", parameters);
                            break;
                        case SQL.Function.Aggregate.Distinct:
                            sbQuery.AppendFormat("SELECT COUNT({0}) FROM transactions.recDevolutionProcess", column);
                            sbQuery.AppendFormat(@"     inner join transactions.recDevolutionProcessDetail
                                                        on recDevolutionProcessDetail.DevolutionProcessID = recDevolutionProcessDetail.DevolutionProcessID {0}", parameters);
                            break;
                        case SQL.Function.Aggregate.Count:
                            sbQuery.AppendFormat("SELECT COUNT({0}) FROM transactions.recDevolutionProcess", column);
                            sbQuery.AppendFormat(@"     inner join transactions.recDevolutionProcessDetail
                                                        on recDevolutionProcessDetail.DevolutionProcessID = recDevolutionProcessDetail.DevolutionProcessID {0}", parameters);
                            break;
                        case SQL.Function.Aggregate.Sum:
                            sbQuery.AppendFormat("SELECT SUM({0}) FROM transactions.recDevolutionProcess", column);
                            sbQuery.AppendFormat(@"     inner join transactions.recDevolutionProcessDetail
                                                        on recDevolutionProcessDetail.DevolutionProcessID = recDevolutionProcessDetail.DevolutionProcessID {0}", parameters);
                            break;
                        case SQL.Function.Aggregate.Avg:
                            sbQuery.AppendFormat("SELECT AVG({0}) FROM transactions.recDevolutionProcess", column);
                            sbQuery.AppendFormat(@"     inner join transactions.recDevolutionProcessDetail
                                                        on recDevolutionProcessDetail.DevolutionProcessID = recDevolutionProcessDetail.DevolutionProcessID {0}", parameters);
                            break;
                        default:
                            // do nothing 
                            break;
                    }

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        _result = cmd.ExecuteScalar();
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return _result;
        }

        public object SelectScalarMonitoringDevolutionProcess(SQL.Function.Aggregate function, string column, string parameters)
        {
            object _result = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    switch (function)
                    {
                        case SQL.Function.Aggregate.Max:
                            sbQuery.AppendFormat("SELECT MAX({0}) FROM transactions.recDevolutionProcess ", column);
                            sbQuery.AppendFormat("    inner join master.recDebtor on recDebtor.DebtorID = recDevolutionProcess.DebtorID {0}", parameters);
                            break;
                        case SQL.Function.Aggregate.Min:
                            sbQuery.AppendFormat("SELECT MIN({0}) FROM transactions.recDevolutionProcess ", column);
                            sbQuery.AppendFormat("    inner join master.recDebtor on recDebtor.DebtorID = recDevolutionProcess.DebtorID {0}", parameters);
                            break;
                        case SQL.Function.Aggregate.Distinct:
                            sbQuery.AppendFormat("SELECT COUNT({0}) FROM transactions.recDevolutionProcess ", column);
                            sbQuery.AppendFormat("    inner join master.recDebtor on recDebtor.DebtorID = recDevolutionProcess.DebtorID {0}", parameters);
                            break;
                        case SQL.Function.Aggregate.Count:
                            sbQuery.AppendFormat("SELECT COUNT({0}) FROM transactions.recDevolutionProcess ", column);
                            sbQuery.AppendFormat("    inner join master.recDebtor on recDebtor.DebtorID = recDevolutionProcess.DebtorID {0}", parameters);
                            break;
                        case SQL.Function.Aggregate.Sum:
                            sbQuery.AppendFormat("SELECT SUM({0}) FROM transactions.recDevolutionProcess ", column);
                            sbQuery.AppendFormat("    inner join master.recDebtor on recDebtor.DebtorID = recDevolutionProcess.DebtorID {0}", parameters);
                            break;
                        case SQL.Function.Aggregate.Avg:
                            sbQuery.AppendFormat("SELECT AVG({0}) FROM transactions.recDevolutionProcess ", column);
                            sbQuery.AppendFormat("    inner join master.recDebtor on recDebtor.DebtorID = recDevolutionProcess.DebtorID {0}", parameters);
                            break;
                        default:
                            // do nothing 
                            break;
                    }

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        _result = cmd.ExecuteScalar();
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return _result;
        }

        public List<recDevolution> GetList(int pageSize, int currentPage, string sortName, string sortOrder, string parameters)
        {
            List<recDevolution> tt = new List<recDevolution>();

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    int startRow = ((currentPage - 1) * pageSize) + 1;
                    int endRow = (startRow + pageSize);

                    StringBuilder sbQuery = new StringBuilder();
                    sbQuery.AppendFormat(" WITH result_set AS ");
                    sbQuery.AppendFormat(" ( ");
                    sbQuery.AppendFormat("    SELECT ");
                    sbQuery.AppendFormat("      ROW_NUMBER() OVER (ORDER BY {0} {1}) AS [row_number], ", sortName, sortOrder);
                    sbQuery.AppendFormat(@"      DevolutionID,
                                                 DebtorID,
                                                 DebtorName,
                                                LetterNo,
                                                LetterDate,
                                                DevolutionDate,
                                                DevolutionValues,
                                                Note");
                    sbQuery.AppendFormat("    FROM ");
                    sbQuery.AppendFormat("      transactions.recDevolution {0}", parameters);
                    sbQuery.AppendFormat(") SELECT * FROM result_set WHERE [row_number] BETWEEN {0} AND {1} ", startRow, endRow);

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recDevolution t = new recDevolution();
                                t.DevolutionID = dr[recDevolution.Fields.DevolutionID] == DBNull.Value ? 0 : int.Parse(dr[recDevolution.Fields.DevolutionID].ToString());
                                t.DebtorID = dr[recDevolution.Fields.DebtorID] == DBNull.Value ? 0 : int.Parse(dr[recDevolution.Fields.DebtorID].ToString());
                                t.DebtorName = dr[recDevolution.Fields.DebtorName] == DBNull.Value ? string.Empty : dr[recDevolution.Fields.DebtorName].ToString();
                                t.LetterNo = dr[recDevolution.Fields.LetterNo] == DBNull.Value ? string.Empty : dr[recDevolution.Fields.LetterNo].ToString();
                                t.LetterDate = dr[recDevolution.Fields.LetterDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recDevolution.Fields.LetterDate].ToString());
                                t.DevolutionDate = dr[recDevolution.Fields.DevolutionDate] == DBNull.Value ? (DateTime)(DateTime?)null : DateTime.Parse(dr[recDevolution.Fields.DevolutionDate].ToString());
                                t.DevolutionValues = dr[recDevolution.Fields.DevolutionValues] == DBNull.Value ? (decimal?)0 : decimal.Parse(dr[recDevolution.Fields.DevolutionValues].ToString());
                                t.Note = dr[recDevolution.Fields.Note] == DBNull.Value ? string.Empty : dr[recDevolution.Fields.Note].ToString();

                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public List<recDevolutionProcess> GetListMonitoringDevolutionProcess(int pageSize, int currentPage, string sortName, string sortOrder, string parameters)
        {
            List<recDevolutionProcess> tt = new List<recDevolutionProcess>();

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    int startRow = ((currentPage - 1) * pageSize) + 1;
                    int endRow = (startRow + pageSize);

                    StringBuilder sbQuery = new StringBuilder();
                    sbQuery.AppendFormat(" WITH result_set AS ");
                    sbQuery.AppendFormat(" ( ");
                    sbQuery.AppendFormat("    SELECT ");
                    sbQuery.AppendFormat("      ROW_NUMBER() OVER (ORDER BY {0} {1}) AS [row_number], ", sortName, sortOrder);
                    sbQuery.AppendFormat(@"      recDevolutionProcess.DevolutionProcessID,
                                                 recDebtor.DebtorCode,
                                                recDebtor.DebtorName,
                                                recDevolutionProcess.LetterDevolutionDate,
                                                recDevolutionProcess.LetterDevolutionNo,
                                                recDevolutionProcess.DevolutionValues");
                    sbQuery.AppendFormat("    FROM ");
                    sbQuery.AppendFormat("      transactions.recDevolutionProcess ");
                    sbQuery.AppendFormat("      inner join master.recDebtor on recDebtor.DebtorID = recDevolutionProcess.DebtorID {0}", parameters);
                    sbQuery.AppendFormat(") SELECT * FROM result_set WHERE [row_number] BETWEEN {0} AND {1} ", startRow, endRow);

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recDevolutionProcess t = new recDevolutionProcess(true);
                                t.row_number = dr[0] == DBNull.Value ? 0 : int.Parse(dr[0].ToString());
                                t.id = dr[recDevolutionProcess.Fields.DevolutionProcessID] == DBNull.Value ? 0 : int.Parse(dr[recDevolutionProcess.Fields.DevolutionProcessID].ToString());
                                t.recDebtor.DebtorCode = dr[recDebtor.Fields.DebtorCode] == DBNull.Value ? string.Empty : dr[recDebtor.Fields.DebtorCode].ToString();
                                t.DebtorName = dr[recDevolutionProcess.Fields.DebtorName] == DBNull.Value ? string.Empty : dr[recDevolutionProcess.Fields.DebtorName].ToString();
                                t.LetterDevolutionNo = dr[recDevolutionProcess.Fields.LetterDevolutionNo] == DBNull.Value ? string.Empty : dr[recDevolutionProcess.Fields.LetterDevolutionNo].ToString();
                                t.LetterDevolutionDate = dr[recDevolutionProcess.Fields.LetterDevolutionDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recDevolutionProcess.Fields.LetterDevolutionDate].ToString());
                                t.DevolutionValues = dr[recDevolutionProcess.Fields.DevolutionValues] == DBNull.Value ? (decimal?)0 : decimal.Parse(dr[recDevolutionProcess.Fields.DevolutionValues].ToString());

                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public List<recDevolutionProcess> GetListDevolutionProcess(int pageSize, int currentPage, string sortName, string sortOrder, string parameters)
        {
            List<recDevolutionProcess> tt = new List<recDevolutionProcess>();

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    int startRow = ((currentPage - 1) * pageSize) + 1;
                    int endRow = (startRow + pageSize);

                    StringBuilder sbQuery = new StringBuilder();
                    sbQuery.AppendFormat(" WITH result_set AS ");
                    sbQuery.AppendFormat(" ( ");
                    sbQuery.AppendFormat("    SELECT ");
                    sbQuery.AppendFormat("      ROW_NUMBER() OVER (ORDER BY recDevolutionProcessDetail.DevolutionProcessDetailID,{0} {1}) AS [row_number], ", sortName, sortOrder);
                    sbQuery.AppendFormat(@"     recDevolutionProcessDetail.DevolutionProcessDetailID,
                                                recDevolutionProcess.DevolutionProcessID,
                                                recDevolutionProcess.DebtorID,
                                                recDevolutionProcess.DebtorName,
                                                recDevolutionProcess.LetterDevolutionNo,
                                                recDevolutionProcess.LetterDevolutionDate,
                                                recDevolutionProcess.DevolutionDate,
                                                recDevolutionProcess.LastDevaluationAction,
                                                recDevolutionProcessDetail.LetterDevProcessDetailNo,
                                                recDevolutionProcessDetail.LetterDevProcessDetailDate");
                    sbQuery.AppendFormat("    FROM ");
                    sbQuery.AppendFormat(@"      transactions.recDevolutionProcess
                                                inner join transactions.recDevolutionProcessDetail 
                                                on recDevolutionProcessDetail.DevolutionProcessID = recDevolutionProcessDetail.DevolutionProcessID");
                    sbQuery.AppendFormat(" {0} ) ", parameters);
                    sbQuery.AppendFormat(" SELECT * FROM result_set WHERE [row_number] BETWEEN {0} AND {1} ", startRow, endRow);

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recDevolutionProcess t = new recDevolutionProcess(true);
                                t.row_number = dr[0] == DBNull.Value ? 0 : int.Parse(dr[0].ToString());
                                t.recDevolutionProcessDetail.DevolutionProcessDetailID = dr[recDevolutionProcessDetail.Fields.DevolutionProcessDetailID] == DBNull.Value ? 0 : int.Parse(dr[recDevolutionProcessDetail.Fields.DevolutionProcessDetailID].ToString());
                                t.DevolutionProcessID = dr[recDevolutionProcess.Fields.DevolutionProcessID] == DBNull.Value ? 0 : int.Parse(dr[recDevolutionProcess.Fields.DevolutionProcessID].ToString());
                                t.DebtorID = dr[recDevolutionProcess.Fields.DebtorID] == DBNull.Value ? 0 : int.Parse(dr[recDevolutionProcess.Fields.DebtorID].ToString());
                                t.DebtorName = dr[recDevolutionProcess.Fields.DebtorName] == DBNull.Value ? string.Empty : dr[recDevolutionProcess.Fields.DebtorName].ToString();
                                t.LetterDevolutionNo = dr[recDevolutionProcess.Fields.LetterDevolutionNo] == DBNull.Value ? string.Empty : dr[recDevolutionProcess.Fields.LetterDevolutionNo].ToString();
                                t.LetterDevolutionDate = dr[recDevolutionProcess.Fields.LetterDevolutionDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recDevolutionProcess.Fields.LetterDevolutionDate].ToString());
                                t.DevolutionDate = dr[recDevolutionProcess.Fields.DevolutionDate] == DBNull.Value ? (DateTime)(DateTime?)null : DateTime.Parse(dr[recDevolutionProcess.Fields.DevolutionDate].ToString());
                                t.LastDevaluationAction = dr[recDevolutionProcess.Fields.DevolutionDate] == DBNull.Value ? string.Empty : dr[recDevolutionProcess.Fields.LastDevaluationAction].ToString();
                                t.recDevolutionProcessDetail.LetterDevProcessDetailNo = dr[recDevolutionProcessDetail.Fields.LetterDevProcessDetailNo] == DBNull.Value ? string.Empty : dr[recDevolutionProcessDetail.Fields.LetterDevProcessDetailNo].ToString();
                                t.recDevolutionProcessDetail.LetterDevProcessDetailDate = dr[recDevolutionProcessDetail.Fields.LetterDevProcessDetailDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recDevolutionProcessDetail.Fields.LetterDevProcessDetailDate].ToString());

                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public recDevolution GetAttachFileByDevolutionId(int devolutionId)
        {
            recDevolution t = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select AttachFileName,AttachFile from transactions.recDevolution WHERE DevolutionID = @DevolutionID";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@DevolutionID", SqlDbType.Int).Value = devolutionId;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                t = new recDevolution();
                                t.AttachFileName = dr[recDevolution.Fields.AttachFileName] == DBNull.Value ? string.Empty : dr[recDevolution.Fields.AttachFileName].ToString();
                                t.AttachFile = dr[recDevolution.Fields.AttachFile] == DBNull.Value ? null : (byte[])(dr[recDevolution.Fields.AttachFile]);

                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return t;
        }

        public recDevolutionProcessDetail GetAttachFileByDevolutionProcessDetailId(int devolutionProcessDetailId)
        {
            recDevolutionProcessDetail t = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select AttachFileName,AttachFile from transactions.recDevolutionProcessDetail WHERE DevolutionProcessDetailID = @DevolutionProcessDetailID";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@DevolutionProcessDetailID", SqlDbType.Int).Value = devolutionProcessDetailId;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                t = new recDevolutionProcessDetail();
                                t.AttachFileName = dr[recDevolution.Fields.AttachFileName] == DBNull.Value ? string.Empty : dr[recDevolution.Fields.AttachFileName].ToString();
                                t.AttachFile = dr[recDevolution.Fields.AttachFile] == DBNull.Value ? null : (byte[])(dr[recDevolution.Fields.AttachFile]);

                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return t;
        }

        public List<recDevolutionProcess> GetListDebtorForRecDevolutionProcess()
        {
            List<recDevolutionProcess> tt = new List<recDevolutionProcess>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    sbQuery.AppendFormat("    SELECT ");
                    sbQuery.AppendFormat(@"  distinct recDebtor.DebtorID
                                            ,recDebtor.DebtorCode
                                            ,recDebtor.DebtorName");
                    sbQuery.AppendFormat("    FROM ");
                    sbQuery.AppendFormat(@"      transactions.recDevolutionProcess
                                                    inner join master.recDebtor on recDebtor.DebtorID = recDevolutionProcess.DebtorID");


                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recDevolutionProcess t = new recDevolutionProcess(true);
                                t.DebtorID = dr[recDevolutionProcess.Fields.DebtorID] == DBNull.Value ? 0 : int.Parse(dr[recDevolutionProcess.Fields.DebtorID].ToString());
                                t.DebtorName = dr[recDevolutionProcess.Fields.DebtorName] == DBNull.Value ? string.Empty : dr[recDevolutionProcess.Fields.DebtorName].ToString();
                                t.recDebtor.DebtorCode = dr[recDebtor.Fields.DebtorCode] == DBNull.Value ? string.Empty : dr[recDebtor.Fields.DebtorCode].ToString();

                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public recDemand GetDemandForDebtorName(int debtorId)
        {
            recDemand t = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"select 
                                        recDemand.DemandNo,
                                        recDemand.DebtorName,
                                        recDemandDetail.LatterNo,
                                        recDemandDetail.LatterDate,
                                        recDemandDetail.TransValue
                                        from transactions.recDemand
                                        inner join transactions.recDemandDetail on recDemandDetail.DemandNo = recDemand.DemandNo
                                        where recDemandDetail.DemandDetailNo = 
                                        (select
                                        MAX(recDemandDetail.DemandDetailNo)
                                        from transactions.recDemand
                                        inner join transactions.recDemandDetail on recDemandDetail.DemandNo = recDemand.DemandNo
                                        where recDemand.DebtorID = @DebtorId)";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@DebtorId", SqlDbType.VarChar).Value = debtorId;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                t = new recDemand(true);
                                t.DemandNo = dr[recDemand.Fields.DemandNo] == DBNull.Value ? 0 : int.Parse(dr[recDemand.Fields.DemandNo].ToString());
                                t.DebtorName = dr[recDemand.Fields.DebtorName] == DBNull.Value ? string.Empty : dr[recDemand.Fields.DebtorName].ToString();
                                t.recDemandDetail.LatterNo = dr[recDemandDetail.Fields.LatterNo] == DBNull.Value ? string.Empty : dr[recDemandDetail.Fields.LatterNo].ToString();
                                t.recDemandDetail.LatterDate = dr[recDemandDetail.Fields.LatterDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recDemandDetail.Fields.LatterDate].ToString());
                                t.recDemandDetail.TransValue = dr[recDemandDetail.Fields.TransValue] == DBNull.Value ? (Decimal?)0 : Decimal.Parse(dr[recDemandDetail.Fields.TransValue].ToString());
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return t;
        }

        public recDevolutionProcess GetByIdDevolutionProcess(int key, string lastDevolutionAction)
        {
            recDevolutionProcess t = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"select
                                        (select DevolutionID from master.recDevolutionAction 
                                        where DevolutionName = 
                                        (select recDevolutionProcess.LastDevaluationAction from transactions.recDevolutionProcess
                                        inner join transactions.recDevolutionProcessDetail
                                        on recDevolutionProcessDetail.DevolutionProcessID = recDevolutionProcess.DevolutionProcessID
                                        where DevolutionProcessDetailID = @DevolutionProcessDetailID)) as DevolutionID,
                                        recDevolutionProcess.DevolutionProcessID,
                                        recDevolutionProcess.DebtorID,
                                        recDevolutionProcess.DebtorName,
                                        recDevolutionProcess.LetterDevolutionNo,
                                        recDevolutionProcess.LetterDevolutionDate,
                                        recDevolutionProcess.DevolutionDate,
                                        recDevolutionProcessDetail.LetterDevProcessDetailNo,
                                        recDevolutionProcessDetail.LetterDevProcessDetailDate
                                        from transactions.recDevolutionProcess
                                        inner join transactions.recDevolutionProcessDetail
                                        on recDevolutionProcessDetail.DevolutionProcessID = recDevolutionProcess.DevolutionProcessID
                                        where recDevolutionProcessDetail.DevolutionProcessDetailID = @DevolutionProcessDetailID";

                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@DevolutionProcessDetailID", SqlDbType.VarChar).Value = key;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                t = new recDevolutionProcess(true);
                                t.DevolutionProcessID = dr[recDevolutionProcess.Fields.DevolutionProcessID] == DBNull.Value ? 0 : int.Parse(dr[recDevolutionProcess.Fields.DevolutionProcessID].ToString());
                                t.DebtorID = dr[recDevolutionProcess.Fields.DebtorID] == DBNull.Value ? 0 : int.Parse(dr[recDevolutionProcess.Fields.DebtorID].ToString());
                                t.DebtorName = dr[recDevolutionProcess.Fields.DebtorName] == DBNull.Value ? string.Empty : dr[recDevolutionProcess.Fields.DebtorName].ToString();
                                t.LetterDevolutionNo = dr[recDevolutionProcess.Fields.LetterDevolutionNo] == DBNull.Value ? string.Empty : dr[recDevolutionProcess.Fields.LetterDevolutionNo].ToString();
                                t.LetterDevolutionDate = dr[recDevolutionProcess.Fields.LetterDevolutionDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recDevolutionProcess.Fields.LetterDevolutionDate].ToString());
                                t.DevolutionDate = dr[recDevolutionProcess.Fields.DevolutionDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recDevolutionProcess.Fields.DevolutionDate].ToString());
                                if (!lastDevolutionAction.Equals(string.Empty))
                                {
                                    t.recDevolutionAction.DevolutionID = dr[0] == DBNull.Value ? 0 : int.Parse(dr[0].ToString());
                                }
                                t.recDevolutionProcessDetail.LetterDevProcessDetailNo = dr[recDevolutionProcessDetail.Fields.LetterDevProcessDetailNo] == DBNull.Value ? string.Empty : dr[recDevolutionProcessDetail.Fields.LetterDevProcessDetailNo].ToString();
                                t.recDevolutionProcessDetail.LetterDevProcessDetailDate = dr[recDevolutionProcessDetail.Fields.LetterDevProcessDetailDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recDevolutionProcessDetail.Fields.LetterDevProcessDetailDate].ToString());

                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return t;
        }

        public string cekLastDevolutionAction(int key)
        {
            string t = string.Empty;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"select
                                        LastDevaluationAction from transactions.recDevolutionProcess
                                        inner join transactions.recDevolutionProcessDetail
                                        on recDevolutionProcessDetail.DevolutionProcessID = recDevolutionProcess.DevolutionProcessID
                                        where DevolutionProcessDetailID = @DevolutionProcessDetailID";

                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@DevolutionProcessDetailID", SqlDbType.VarChar).Value = key;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                string lastDevaluationAction = dr[recDevolutionProcess.Fields.LastDevaluationAction] == DBNull.Value ? string.Empty : dr[recDevolutionProcess.Fields.LastDevaluationAction].ToString();
                                t = lastDevaluationAction;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return t;
        }

        public List<recDevolutionAction> GetListDevolutionAction(int DevolutionProcessDetailId)
        {
            List<recDevolutionAction> tt = new List<recDevolutionAction>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"select
                                        DevolutionID,
                                        DevolutionCode,
                                        DevolutionName
                                         from master.recDevolutionAction
                                         where DevolutionCode not in 
                                         (
                                        select
                                        recDevolutionProcessDetail.DevolutionActionCode
                                        from
                                        transactions.recDevolution
                                        inner join transactions.recDevolutionProcess on recDevolutionProcess.DevolutionID = recDevolution.DevolutionID
                                          inner join transactions.recDevolutionProcessDetail 
                                          on recDevolutionProcessDetail.DevolutionProcessID = recDevolutionProcess.DevolutionProcessID
                                          where recDevolutionProcess.DevolutionProcessID = (select 
                                        recDevolutionProcess.DevolutionProcessID
                                        from 
                                        transactions.recDevolution
                                        inner join transactions.recDevolutionProcess on recDevolutionProcess.DevolutionID = recDevolution.DevolutionID
                                          inner join transactions.recDevolutionProcessDetail 
                                          on recDevolutionProcessDetail.DevolutionProcessID = recDevolutionProcess.DevolutionProcessID
                                                where recDevolutionProcessDetail.DevolutionProcessDetailID = " + DevolutionProcessDetailId + "))";

                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recDevolutionAction t = new recDevolutionAction();
                                t.DevolutionID = dr[recDevolutionAction.Fields.DevolutionID] == DBNull.Value ? 0 : int.Parse(dr[recDevolutionAction.Fields.DevolutionID].ToString());
                                t.DevolutionCode = dr[recDevolutionAction.Fields.DevolutionCode] == DBNull.Value ? string.Empty : dr[recDevolutionAction.Fields.DevolutionCode].ToString();
                                t.DevolutionName = dr[recDevolutionAction.Fields.DevolutionName] == DBNull.Value ? string.Empty : dr[recDevolutionAction.Fields.DevolutionName].ToString();
                                
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }		

        public List<recDebtor> GetDebtorForRecDevolution()
        {
            List<recDebtor> tt = new List<recDebtor>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    sbQuery.AppendFormat("    SELECT ");
                    sbQuery.AppendFormat(@"     DISTINCT recDebtor.DebtorID, recDebtor.DebtorCode,
                                                recDebtor.DebtorName");
                    sbQuery.AppendFormat("    FROM ");
                    sbQuery.AppendFormat(@"      transactions.recDemand
                                                inner join master.recDebtor on recDebtor.DebtorID = recDemand.DebtorID
                                                inner join transactions.recDemandDetail on recDemandDetail.DemandNo = recDemand.DemandNo
                                                where recDemand.IsActive = 0 and recDemand.LastAction = 'Pelimpahan Ke DJKN'
                                                and recDemandDetail.BillingNo not in(select BillingNo from transactions.recDevolution)");

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recDebtor t = new recDebtor();
                                t.DebtorID = dr[recDebtor.Fields.DebtorID] == DBNull.Value ? 0 : int.Parse(dr[recDebtor.Fields.DebtorID].ToString());
                                t.DebtorCode = dr[recDebtor.Fields.DebtorCode] == DBNull.Value ? string.Empty : dr[recDebtor.Fields.DebtorCode].ToString();
                                t.DebtorName = dr[recDebtor.Fields.DebtorName] == DBNull.Value ? string.Empty : dr[recDebtor.Fields.DebtorName].ToString();

                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public bool Insert(recDevolution domain)
        {
            bool result = false;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                    conn.Open();
                    SqlTransaction txn = conn.BeginTransaction();
                    try
                    {
                        //mencari nomor billing
                        SqlCommand cmd = conn.CreateCommand();
                        cmd.CommandText = @"
                                select 
                                    BillingNo
                                from
                                    transactions.recDemandDetail
                                    where DemandNo = " + domain.DemandNo;

                        cmd.CommandType = CommandType.Text;
                        cmd.Transaction = txn;
                        var temp1 = cmd.ExecuteScalar();
                        string billingNo = temp1.ToString();
                        cmd.Dispose();

                        //insert recDevolution
                        cmd = conn.CreateCommand();
                        cmd.CommandText = "INSERT INTO transactions.recDevolution(DemandNo, BillingNo, DebtorID, DebtorName, LetterNo, LetterDate, DevolutionDate, DevolutionValues, Balance, Note, IsProcess, IsActive, AttachFile, AttachFileName, CreatedDate, CreatedBy, UpdatedDate, UpdatedBy) VALUES(@DemandNo, @BillingNo, @DebtorID, @DebtorName, @LetterNo, @LetterDate, @DevolutionDate, @DevolutionValues, @Balance, @Note, @IsProcess, @IsActive, @AttachFile, @AttachFileName, @CreatedDate, @CreatedBy, @UpdatedDate, @UpdatedBy)";
                        cmd.CommandType = CommandType.Text;
                        cmd.Parameters.Add("@DemandNo", SqlDbType.Int).Value = domain.DemandNo;
                        cmd.Parameters.Add("@BillingNo", SqlDbType.VarChar).Value = billingNo;
                        cmd.Parameters.Add("@DebtorID", SqlDbType.Int).Value = domain.DebtorID;
                        cmd.Parameters.Add("@DebtorName", SqlDbType.VarChar).Value = domain.DebtorName;
                        cmd.Parameters.Add("@LetterNo", SqlDbType.VarChar).Value = (object)domain.LetterNo ?? DBNull.Value;
                        cmd.Parameters.Add("@LetterDate", SqlDbType.DateTime).Value = (object)domain.LetterDate ?? DBNull.Value;
                        cmd.Parameters.Add("@DevolutionDate", SqlDbType.DateTime).Value = (object)domain.DevolutionDate ?? DBNull.Value;
                        cmd.Parameters.Add("@DevolutionValues", SqlDbType.Decimal).Value = (object)domain.DevolutionValues ?? DBNull.Value;
                        cmd.Parameters.Add("@Balance", SqlDbType.Decimal).Value = (object)domain.Balance ?? DBNull.Value;
                        cmd.Parameters.Add("@Note", SqlDbType.VarChar).Value = (object)domain.Note ?? DBNull.Value;
                        cmd.Parameters.Add("@IsProcess", SqlDbType.Bit).Value = (object)domain.IsProcess ?? DBNull.Value;
                        cmd.Parameters.Add("@IsActive", SqlDbType.Bit).Value = (object)domain.IsActive ?? DBNull.Value;
                        cmd.Parameters.Add("@AttachFile", SqlDbType.VarBinary).Value = (object)domain.AttachFile ?? DBNull.Value;
                        cmd.Parameters.Add("@AttachFileName", SqlDbType.VarChar).Value = (object)domain.AttachFileName ?? DBNull.Value;
                        cmd.Parameters.Add("@CreatedDate", SqlDbType.DateTime).Value = (object)domain.CreatedDate ?? DBNull.Value;
                        cmd.Parameters.Add("@CreatedBy", SqlDbType.VarChar).Value = (object)domain.CreatedBy ?? DBNull.Value;
                        cmd.Parameters.Add("@UpdatedDate", SqlDbType.DateTime).Value = (object)domain.UpdatedDate ?? DBNull.Value;
                        cmd.Parameters.Add("@UpdatedBy", SqlDbType.VarChar).Value = (object)domain.UpdatedBy ?? DBNull.Value;

                        cmd.Transaction = txn;
                        cmd.ExecuteNonQuery(); // insert recDevolution
                        cmd.Dispose();

                        cmd = conn.CreateCommand();
                        cmd.CommandText = @"
                                update transactions.recBilling
                                set IsDjkn = 1
                                where BillingNo = '" + billingNo + "'";

                        cmd.Transaction = txn;
                        cmd.ExecuteNonQuery(); // update recBilling  isDJKN = 1
                        cmd.Dispose();

                        result = true;
                        txn.Commit();
                    }
                    catch (Exception ex)
                    {
                        txn.Rollback();
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
                    
            return result;
        }

        public bool insertDevolutionProcessDetail(recDevolutionProcess domain)
        {
            bool result = false;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlTransaction txn = conn.BeginTransaction();
                try
                {
                    //mencari devolutionCode 
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = @"
                                select
                                    DevolutionCode
                                from 
                                    master.recDevolutionAction
                                where DevolutionID = " + domain.recDevolutionAction.DevolutionID;

                    cmd.CommandType = CommandType.Text;
                    cmd.Transaction = txn;
                    var temp1 = cmd.ExecuteScalar();
                    string devolutionCode = temp1.ToString();
                    cmd.Dispose();

                    //mencari devolutionCode 
                    cmd = conn.CreateCommand();
                    cmd.CommandText = @"
                                select
                                    DevolutionName
                                from 
                                    master.recDevolutionAction
                                where DevolutionID = " + domain.recDevolutionAction.DevolutionID;

                    cmd.CommandType = CommandType.Text;
                    cmd.Transaction = txn;
                    var temp2 = cmd.ExecuteScalar();
                    string devolutionName = temp2.ToString();
                    cmd.Dispose();

                    cmd = conn.CreateCommand();
                    cmd.CommandText = "INSERT INTO transactions.recDevolutionProcessDetail(DevolutionProcessID, LetterDevProcessDetailNo, LetterDevProcessDetailDate, DevolutionActionCode, DevolutionActionName, AttachFileName, AttachFile, CreatedDate, CreatedBy, UpdatedDate, UpdatedBy) VALUES(@DevolutionProcessID, @LetterDevProcessDetailNo, @LetterDevProcessDetailDate, @DevolutionActionCode, @DevolutionActionName, @AttachFileName, @AttachFile, @CreatedDate, @CreatedBy, @UpdatedDate, @UpdatedBy)";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@DevolutionProcessID", SqlDbType.Int).Value = (object)domain.DevolutionProcessID ?? DBNull.Value;
                    cmd.Parameters.Add("@LetterDevProcessDetailNo", SqlDbType.VarChar).Value = (object)domain.recDevolutionProcessDetail.LetterDevProcessDetailNo ?? DBNull.Value;
                    cmd.Parameters.Add("@LetterDevProcessDetailDate", SqlDbType.DateTime).Value = (object)domain.recDevolutionProcessDetail.LetterDevProcessDetailDate ?? DBNull.Value;
                    cmd.Parameters.Add("@DevolutionActionCode", SqlDbType.VarChar).Value = devolutionCode;
                    cmd.Parameters.Add("@DevolutionActionName", SqlDbType.VarChar).Value = devolutionName;
                    cmd.Parameters.Add("@AttachFileName", SqlDbType.VarChar).Value = (object)domain.recDevolutionProcessDetail.AttachFileName ?? DBNull.Value;
                    cmd.Parameters.Add("@AttachFile", SqlDbType.VarBinary).Value = (object)domain.recDevolutionProcessDetail.AttachFile ?? DBNull.Value;
                    cmd.Parameters.Add("@CreatedDate", SqlDbType.DateTime).Value = (object)domain.recDevolutionProcessDetail.CreatedDate ?? DBNull.Value;
                    cmd.Parameters.Add("@CreatedBy", SqlDbType.VarChar).Value = (object)domain.recDevolutionProcessDetail.CreatedBy ?? DBNull.Value;
                    cmd.Parameters.Add("@UpdatedDate", SqlDbType.DateTime).Value = (object)domain.recDevolutionProcessDetail.UpdatedDate ?? DBNull.Value;
                    cmd.Parameters.Add("@UpdatedBy", SqlDbType.VarChar).Value = (object)domain.recDevolutionProcessDetail.UpdatedBy ?? DBNull.Value;

                    cmd.Transaction = txn;
                    cmd.ExecuteNonQuery(); // insert recDemandDetail
                    cmd.Dispose();

                    //update recDevolutionProcess lastAction
                    cmd = conn.CreateCommand();
                    cmd.CommandText = "update transactions.recDevolutionProcess set LastDevaluationAction = @LastDevaluationAction where DevolutionProcessID = @DevolutionProcessID";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@LastDevaluationAction", SqlDbType.VarChar).Value = devolutionName;
                    cmd.Parameters.Add("@DevolutionProcessID", SqlDbType.Int).Value = domain.DevolutionProcessID;

                    cmd.Transaction = txn;
                    cmd.ExecuteNonQuery(); // update recDemand lastAction
                    cmd.Dispose();

                    
                    result = true;
                    txn.Commit();
                }
                catch (Exception ex)
                {
                    txn.Rollback();
                    throw (ex);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open) conn.Close();
                }
            }

            return result;
        }

        public bool updateDevolutionProcessDetail(recDevolutionProcess domain)
        {
            bool result = false;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlTransaction txn = conn.BeginTransaction();
                try
                {
                    //mencari devolutionCode 
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = @"
                                select
                                    DevolutionCode
                                from 
                                    master.recDevolutionAction
                                where DevolutionID = " + domain.recDevolutionAction.DevolutionID;

                    cmd.CommandType = CommandType.Text;
                    cmd.Transaction = txn;
                    var temp1 = cmd.ExecuteScalar();
                    string devolutionCode = temp1.ToString();
                    cmd.Dispose();

                    //mencari devolutionCode 
                    cmd = conn.CreateCommand();
                    cmd.CommandText = @"
                                select
                                    DevolutionName
                                from 
                                    master.recDevolutionAction
                                where DevolutionID = " + domain.recDevolutionAction.DevolutionID;

                    cmd.CommandType = CommandType.Text;
                    cmd.Transaction = txn;
                    var temp2 = cmd.ExecuteScalar();
                    string devolutionName = temp2.ToString();
                    cmd.Dispose();

                    cmd = conn.CreateCommand();
                    cmd.CommandText = "UPDATE transactions.recDevolutionProcessDetail SET DevolutionProcessID = @DevolutionProcessID, LetterDevProcessDetailNo = @LetterDevProcessDetailNo, LetterDevProcessDetailDate = @LetterDevProcessDetailDate, DevolutionActionCode = @DevolutionActionCode, DevolutionActionName = @DevolutionActionName, AttachFileName = @AttachFileName, AttachFile = @AttachFile, UpdatedDate = @UpdatedDate, UpdatedBy = @UpdatedBy WHERE DevolutionProcessDetailID = @DevolutionProcessDetailID";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@DevolutionProcessDetailID", SqlDbType.Int).Value = domain.recDevolutionProcessDetail.DevolutionProcessDetailID;
                    cmd.Parameters.Add("@DevolutionProcessID", SqlDbType.Int).Value = (object)domain.DevolutionProcessID ?? DBNull.Value;
                    cmd.Parameters.Add("@LetterDevProcessDetailNo", SqlDbType.VarChar).Value = (object)domain.recDevolutionProcessDetail.LetterDevProcessDetailNo ?? DBNull.Value;
                    cmd.Parameters.Add("@LetterDevProcessDetailDate", SqlDbType.DateTime).Value = (object)domain.recDevolutionProcessDetail.LetterDevProcessDetailDate ?? DBNull.Value;
                    cmd.Parameters.Add("@DevolutionActionCode", SqlDbType.VarChar).Value = devolutionCode;
                    cmd.Parameters.Add("@DevolutionActionName", SqlDbType.VarChar).Value = devolutionName;
                    cmd.Parameters.Add("@AttachFileName", SqlDbType.VarChar).Value = (object)domain.recDevolutionProcessDetail.AttachFileName ?? DBNull.Value;
                    cmd.Parameters.Add("@AttachFile", SqlDbType.VarBinary).Value = (object)domain.recDevolutionProcessDetail.AttachFile ?? DBNull.Value;
                    cmd.Parameters.Add("@UpdatedDate", SqlDbType.DateTime).Value = (object)domain.recDevolutionProcessDetail.UpdatedDate ?? DBNull.Value;
                    cmd.Parameters.Add("@UpdatedBy", SqlDbType.VarChar).Value = (object)domain.recDevolutionProcessDetail.UpdatedBy ?? DBNull.Value;

                    cmd.Transaction = txn;
                    cmd.ExecuteNonQuery(); // insert recDemandDetail
                    cmd.Dispose();

                    //update recDevolutionProcess lastAction
                    cmd = conn.CreateCommand();
                    cmd.CommandText = "update transactions.recDevolutionProcess set LastDevaluationAction = @LastDevaluationAction where DevolutionProcessID = @DevolutionProcessID";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@LastDevaluationAction", SqlDbType.VarChar).Value = devolutionName;
                    cmd.Parameters.Add("@DevolutionProcessID", SqlDbType.Int).Value = domain.DevolutionProcessID;

                    cmd.Transaction = txn;
                    cmd.ExecuteNonQuery(); // update recDemand lastAction
                    cmd.Dispose();


                    result = true;
                    txn.Commit();
                }
                catch (Exception ex)
                {
                    txn.Rollback();
                    throw (ex);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open) conn.Close();
                }
            }

            return result;
        }

        public bool insertRecDevolutionProcess(string userLogin)
        {
            bool result = false;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "exec [dbo].[SP_insertDevolution] @userLogin";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@userLogin", SqlDbType.VarChar).Value = userLogin;

                    try
                    {
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        result = true;
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }

                }
            }
            return result;
        }

    }
}
